import Router from 'koa-router'
import axios from './utils/axios'
import Poi from '../dbs/models/poi'
// 定义接口路由前缀
let router = new Router({
    prefix: '/search'
})

const sign = 'f8170bc5521c6390c866846ba6fb00a4'

router.get('/top', async (ctx) => {
    let { status, data: { top } } = await axios.get(`http://cp-tools.cn/search/top`,{
        params:{
            input:ctx.query.input,
            city:ctx.query.city,
            sign
        }
    })
        ctx.body={
          top:status===200?top:[]
        }
})

router.get('/hotPlace',async(ctx)=>{
    let city=ctx.store?ctx.store.geo.positon.city:ctx.query.city 
    let {status,data:{result}}=await axios.get(`http://cp-tools.cn/search/hotPlace`,{
        params:{
            sign,
            city
        }
    })
    ctx.body={
        result:status===200?result:[]
    }
})


router.get('/resultsByKeywords',async (ctx)=>{
    const {city,keyword}=ctx.query;
    let {status,data:{count,pois}}=await axios.get(`http://cp-tools.cn/search/resultsByKeywords`,{
        params:{
            sign,
            city,
            keyword
        }
    })
    ctx.body={
        count:status===200?count:[],
        pois:status===200?pois:[]
    }
})

router.get('/products', async (ctx) => {
    let keyword = ctx.query.keyword || '旅游'
    let city = ctx.query.city || '北京'
    let {
      status,
      data: {
        product,
        more
      }
    } = await axios.get('http://cp-tools.cn/search/products', {
      params: {
        keyword,
        city,
        sign
      }
    })
    if (status === 200) {
      ctx.body = {
        product,
        more: ctx.isAuthenticated() ? more: [],
        login: ctx.isAuthenticated()
      }
    }else{
      ctx.body = {
        product: {},
        more: ctx.isAuthenticated() ? more: [],
        login: ctx.isAuthenticated()
      }
    }
  })
  
// 导出路由
export default router;